﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.OA.BusinessFlowModel.BusinessFlowModel;
using BCService.OA.BusinessFlowModel.BusinessFlowModel;
using BCService.Utils;
using BlackCoffeeRESTful.Configs;

namespace BlackCoffeeRESTful.Controllers.Management.OA.BusinessFlowModel.BusinessFlowModel
{
    /// <summary>
    /// OA系统业务流模型
    /// </summary>
    [ApiExplorerSettings(GroupName = "OAAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class BusinessFlowModelController : ManagementBaseController
    {
        private readonly IBusinessFlowModelService businessFlowModelService;

        public BusinessFlowModelController(IBusinessFlowModelService businessFlowModelService)
        {
            this.businessFlowModelService = businessFlowModelService;
        }

        #region 添加OA系统业务流模型
        /// <summary>
        ///  添加OA系统业务流模型
        /// </summary>
        /// <param name="requestDto">OA系统业务流模型信息</param>
        /// <response code="201">添加OA系统业务流模型成功</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加OA系统业务流模型")]
        [ProducesResponseType(typeof(BusinessFlowModelDto), 201)]
        public IActionResult Post([FromBody]BusinessFlowModelRequestDto requestDto)
        {
            //if (!this.Admin.IsHeadOffice)
            //{
            //    requestDto.IsGroupBusiness = false;
            //}
            var result = this.businessFlowModelService.Add(requestDto);
            
            return this.Created(string.Format("api/v1/sim/BusinessFlowModel/{0}", result.BusinessFlowModelId), result);
        }
        #endregion

        #region 修改OA系统业务流模型
        /// <summary>
        ///  修改OA系统业务流模型
        /// </summary>
        /// <param name="businessFlowModelId">OA系统业务流模型Id</param>
        /// <param name="requestDto">OA系统业务流模型信息</param>
        /// <response code="200">修改OA系统业务流模型成功</response>
        /// <returns></returns>
        [HttpPut("{business_flow_model_id}")]
        [Log("修改OA系统业务流模型")]
        [ProducesResponseType(typeof(BusinessFlowModelDto), 200)]
        public IActionResult Post([FromRoute(Name = "business_flow_model_id")]long businessFlowModelId, [FromBody]BusinessFlowModelRequestDto requestDto)
        {
            //if (!this.Admin.IsHeadOffice)
            //{
            //    requestDto.IsGroupBusiness = false;
            //}
            BusinessFlowModelPutDto putDto = requestDto.As<BusinessFlowModelPutDto>();
            putDto.BusinessFlowModelId = businessFlowModelId;
            return this.Ok(this.businessFlowModelService.Update(putDto));
        }
        #endregion

        #region 删除OA系统业务流模型
        /// <summary>
        ///  删除OA系统业务流模型
        /// </summary>
        /// <param name="businessFlowModelId">OA系统业务流模型Id</param>
        /// <response code="204">删除OA系统业务流模型成功</response>
        /// <returns></returns>
        [HttpDelete("{business_flow_model_id}")]
        [Log("删除OA系统业务流模型")]
        public IActionResult Delete([FromRoute(Name = "business_flow_model_id")]long businessFlowModelId)
        {
            this.businessFlowModelService.Delete(businessFlowModelId);
            return this.NoContent();
        }
        #endregion

        #region 获取OA系统业务流模型
        /// <summary>
        ///  获取OA系统业务流模型
        /// </summary>
        /// <param name="businessFlowModelId">OA系统业务流模型Id</param>
        /// <response code="200">获取OA系统业务流模型</response>
        /// <returns></returns>
        [HttpGet("{business_flow_model_id}")]
        [ProducesResponseType(typeof(BusinessFlowModelDto), 200)]
        public IActionResult Get([FromRoute(Name = "business_flow_model_id")]long businessFlowModelId)
        {
            return this.Ok(this.businessFlowModelService.GetEntity(businessFlowModelId));
        }
        #endregion

        #region 获取OA系统业务流模型列表
        /// <summary>
        ///  获取OA系统业务流模型列表
        /// </summary>
        /// <response code="200">获取OA系统业务流模型列表</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(BusinessFlowModelDto), 200)]
        public IActionResult Get([FromQuery(Name ="company_id")]int? companyId)
        {
            if (!companyId.HasValue)
            {
                companyId = this.Admin.CompanyId;
            }
            //if (!this.Admin.IsHeadOffice)
            //{
            //    companyId = this.Admin.CompanyId;
            //}
            return this.Ok(this.businessFlowModelService.GetList(companyId.Value));
        }
        #endregion

        #region 获取OA系统业务流模型（分页）
        /// <summary>
        ///  获取OA系统业务流模型（分页）
        /// </summary>
        /// <param name="companyId">公司Id</param>
        /// <param name="businessFlowModelName">OA系统业务流模型名称</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <response code="200">获取OA系统业务流模型（分页）</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<BusinessFlowModelDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "company_id")]int? companyId,
            [FromQuery(Name = "business_flow_model_name")]string businessFlowModelName,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            int pageIndex = 1,
            int pageSize = 10)
        {
            if (!companyId.HasValue)
            {
                companyId = this.Admin.CompanyId;
            }
            //if (!this.Admin.IsHeadOffice)
            //{
            //    companyId = this.Admin.CompanyId;
            //}
            return this.Ok(this.businessFlowModelService.Query(companyId.Value,businessFlowModelName, start, end, pageIndex, pageSize));
        }
        #endregion

    }
}
